// #include <iostream>
// #include <cstdio>
// #include <cstdlib>
// #include <string>


// int main()
// {
//     //打开文件
//     FILE* fp = fopen("bite","w");
//     //写文件
//     char buffer[] = "linux so easy";
//     fwrite(buffer, sizeof(char), sizeof(buffer), fp);
//     //读文件
//     char* str;
//     str = (char*)malloc(sizeof(char) * 15);
//     fread(str, sizeof(char), sizeof(buffer), fp);
//     printf("%s\n",str);
//     //关闭文件
//     fclose(fp);


//     FILE* fp2 = fopen("log.txt", "w");
//     fputs("This is an apple", fp2);
//     fseek(fp2, 9 ,SEEK_SET);
//     fputs("same", fp2);
//     fclose(fp2);
//     return 0;
// }

#include <stdio.h>
#include <string.h>


int main()
{
    FILE *fp = fopen("./bite", "wb+");
    if (fp == NULL) {
        perror("fopen error");
        return -1; 
    }   
    fseek(fp, 0, SEEK_SET);
    char *data = "linux so easy!\n";
    //size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
    size_t ret = fwrite(data, 1, strlen(data), fp);
    if (ret != strlen(data)) {
        perror("fwrite error");
        return -1; 
    }   
    fseek(fp, 0, SEEK_SET);//跳转读写位置到，从文件起始位置开始偏移0个字节
    char buf[1024] = {0};
    //size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
    ret = fread(buf, 1, 1023, fp);//因为设置读取块大小位1，块个数为1023因此fread返回值为实际读取到的数据长度
    if (ret == 0) {
        if (ferror(fp)) //判断上一次IO操作是否正确
            printf("fread error\n");
        if (feof(fp)) //判断是否读取到了文件末尾
            printf("read end of file!\n");
        return -1; 
    }   
    printf("%s", buf);
    fclose(fp);
    return 0;
}